import tkinter as tk
from view.order_view import OrderView
from view.trade_view import TradeView
from view.config_view import ConfigView
from PIL import Image, ImageTk
from utils.image_util import base64img
from res.config_click_png import img as config_click
from res.config_png import img as config
from res.order_click_png import img as order_click
from res.order_png import img as order
from res.trade_click_png import img as trade_click
from res.trade_png import img as trade


class PrimeView:

    inputViewWidth = 70
    labelViewWidth = 170

    windowWidth = 800
    windowHigh = 520
    title = "美金收割机"

    def __init__(self):
        self.initViiew()

    def initImage(self):
        self.config_click_img = Image.open(
            base64img(config_click)).convert("RGBA").resize((26, 26))
        self.config_img = Image.open(base64img(config)).convert("RGBA").resize(
            (26, 26))
        self.order_click_img = Image.open(
            base64img(order_click)).convert("RGBA").resize((26, 26))
        self.order_img = Image.open(base64img(order)).convert("RGBA").resize(
            (26, 26))
        self.trade_click_img = Image.open(
            base64img(trade_click)).convert("RGBA").resize((26, 26))
        self.trade_img = Image.open(base64img(trade)).convert("RGBA").resize(
            (26, 26))

    def initViiew(self):
        """
        初始化界面
        """
        self.initImage()

        self.root = tk.Tk()
        x = (self.root.winfo_screenwidth() - self.windowWidth) // 2
        y = (self.root.winfo_screenheight() - self.windowHigh) // 2
        self.root.geometry(f'{self.windowWidth}x{self.windowHigh}+{x}+{y}')

        self.root.configure(background="#ffffff")
        self.root.title(self.title)

        self.root.resizable(width=False, height=False)
        # self.left_menu(self.root)
        self.right_menu(self.root)
        self.on_menu_click(1)

    def left_menu(self, root):
        """
        左侧菜单
        """
        leftFrame = tk.Frame(root, width=200, height=self.windowHigh)
        leftFrame.configure(background="#292929")
        leftFrame.grid(row=0, column=0, sticky="nsew")

        global tradeTkImage
        tradeTkImage = ImageTk.PhotoImage(self.trade_img)
        self.tradeImg = tk.Label(leftFrame, bg="#292929", image=tradeTkImage)
        self.tradeImg.place(x=46, y=205)

        self.tradeCentre = tk.Label(leftFrame,
                                    bg="#292929",
                                    text="交易动态",
                                    font=("Helvetica", 12, "bold"),
                                    foreground="#A7A7A7")
        self.tradeCentre.place(x=80, y=210)
        self.tradeImg.bind("<Button-1>", lambda event: self.on_menu_click(1))
        self.tradeCentre.bind("<Button-1>",
                              lambda event: self.on_menu_click(1))

        global orderTkImage
        orderTkImage = ImageTk.PhotoImage(self.order_img)
        self.orderImg = tk.Label(leftFrame, bg="#292929", image=orderTkImage)
        self.orderImg.place(x=46, y=252)

        self.orderCentre = tk.Label(leftFrame,
                                    bg="#292929",
                                    text="订单统计",
                                    font=("Helvetica", 12, "bold"),
                                    foreground="#A7A7A7")
        self.orderCentre.place(x=80, y=257)
        self.orderImg.bind("<Button-1>", lambda event: self.on_menu_click(2))
        self.orderCentre.bind("<Button-1>",
                              lambda event: self.on_menu_click(2))

        global configTkImage
        configTkImage = ImageTk.PhotoImage(self.config_img)
        self.configImg = tk.Label(leftFrame, bg="#292929", image=configTkImage)
        self.configImg.place(x=46, y=299)

        self.configCentre = tk.Label(leftFrame,
                                     bg="#292929",
                                     text="配置中心",
                                     font=("Helvetica", 12, "bold"),
                                     foreground="#A7A7A7")
        self.configCentre.place(x=80, y=304)
        self.configImg.bind("<Button-1>", lambda event: self.on_menu_click(3))
        self.configCentre.bind("<Button-1>",
                               lambda event: self.on_menu_click(3))

    def right_menu(self, root):
        """
        右侧菜单
        """
        self.rightFrame = tk.Frame(root, width=600, height=self.windowHigh)
        self.rightFrame.configure(background="#121212")
        self.rightFrame.grid(row=0, column=1, sticky="nsew")

    def on_menu_click(self, argument):
        if argument == 1:
            tradeTkImage.paste(self.trade_click_img)
            orderTkImage.paste(self.order_img)
            configTkImage.paste(self.config_img)

            self.tradeCentre.configure(foreground="#FFA07E")
            self.orderCentre.configure(foreground="#A7A7A7")
            self.configCentre.configure(foreground="#A7A7A7")

            TradeView(self.rightFrame)
        elif argument == 2:
            tradeTkImage.paste(self.trade_img)
            orderTkImage.paste(self.order_click_img)
            configTkImage.paste(self.config_img)

            self.tradeCentre.configure(foreground="#A7A7A7")
            self.orderCentre.configure(foreground="#FFA07E")
            self.configCentre.configure(foreground="#A7A7A7")

            OrderView(self.rightFrame)
        elif argument == 3:
            tradeTkImage.paste(self.trade_img)
            orderTkImage.paste(self.order_img)
            configTkImage.paste(self.config_click_img)

            self.tradeCentre.configure(foreground="#A7A7A7")
            self.orderCentre.configure(foreground="#A7A7A7")
            self.configCentre.configure(foreground="#FFA07E")

            ConfigView(self.root, self.rightFrame)
        self.tradeCentre.update()
        self.orderCentre.update()
        self.configCentre.update()

    def show(self):
        self.root.mainloop()
